import verifyField from '@/lib/validation/verifyField'
import { Component, Vue } from 'vue-property-decorator'

@Component
export default class extends Vue {
  // ++++++++++ model ++++++++++
  // ++++++++++ props ++++++++++
  // ++++++++++ data ++++++++++
  form = {
    username: 'ceshi',
    password: 123456,
    checkPassword: 123456,
    type: 'user',
  }

  // ++++++++++ computed ++++++++++
  get rules() {
    return {
      type: verifyField.common('类型不能为空'),
      username: verifyField.username,
      password: verifyField.password,
      checkPassword: verifyField.checkPassword(this.form.password),
    }
  }

  // ++++++++++ methods normal ++++++++++
  // ++++++++++ methods event ++++++++++
  submit() {
    const formref = this.$refs.form as any

    formref.validate(async (valid: boolean) => {
      if (valid) {
        const userinfo = await this.$Api.user.save(this.form)

        if (!userinfo) {
          return this.$message.error('注册失败！')
        }
        this.$message.success('注册成功！')

        this.$router.push('/login')
      }
    })
  }
  // ++++++++++ Lifecycle Hooks ++++++++++
  // ++++++++++ watch ++++++++++
}
